package com.byx.chatclient.utils;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.byx.chatclient.R;


/**
 * @author Superb
 */
public class GlideUtils {

 	public static void loadChatImage(final Context mContext, String imgUrl, final ImageView imageView) {
		final RequestOptions options = new RequestOptions()
				.placeholder(R.mipmap.default_img_failed)// 正在加载中的图片
				.error(R.mipmap.default_img_failed); // 加载失败的图片

		Glide.with(mContext)
				.load(imgUrl) // 图片地址
				.apply(options)
				.into(new SimpleTarget<Drawable>() {
					@Override
					public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
						ImageSize imageSize = BitmapUtil.getImageSize(((BitmapDrawable)resource).getBitmap());
						ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
						layoutParams.width = imageSize.getWidth();
						layoutParams.height = imageSize.getHeight();
						imageView.setLayoutParams(layoutParams);

						Glide.with(mContext)
								.load(resource)
								.apply(options) // 参数
								.into(imageView);
					}
				});
	}

	public static void loadImage(final Context mContext, String imgUrl, final ImageView imageView) {
		final RequestOptions options = new RequestOptions()
				// 正在加载中的图片
				.placeholder(R.mipmap.default_img_failed)
				// 加载失败的图片
				.error(R.mipmap.default_img_failed);

		Glide.with(mContext)
				// 图片地址
				.load(imgUrl)
				.apply(options)
				.into(new SimpleTarget<Drawable>() {
					@Override
					public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
						Glide.with(mContext)
								.load(resource)
								.apply(options) // 参数
								.into(imageView);
					}
				});
	}
}
